﻿using System;
using System.IO;
using System.Windows;
using System.Reflection;
using System.Windows.Controls;

namespace Builderator.Firefly.Controls
{
    public enum UriType { File, Folder };
    /// <summary>
    /// Interaction logic for UriControl.xaml
    /// </summary>
    public partial class UriControl : UserControl, IProperty
    {
        public static DependencyProperty LocationProperty = DependencyProperty.Register("Location", typeof(String), typeof(UriControl));

        public new event PropertyChanged OnPropertyChanged;

        public string Location
        {
            get 
            {
                return (String)this.GetValue(LocationProperty); 
            }
            set 
            {
                SetValue(LocationProperty, value);

                if (value != null)
                    this.uriTextBox.Text = value;

                if (this.OnPropertyChanged != null)
                    this.OnPropertyChanged(this.Property, value);
            }
        }

        public UriType Type
        {
            get;
            set;
        }

        public PropertyInfo Property
        {
            get;
            set;
        }

        public string FileExtensions
        {
            get;
            set;
        }

        public UriControl()
        {
            InitializeComponent();
        }

        public UriControl(UriType type)
            : this()
        {
            this.Type = type;
        }

        public UriControl(PropertyInfo property) 
            : this()
        {
            this.Property = property;
        }

        private void browseButton_Click(object sender, RoutedEventArgs e)
        {
            switch (this.Type)
            {
                case UriType.File:
                    {
                        System.Windows.Forms.OpenFileDialog openDialog = new System.Windows.Forms.OpenFileDialog();

                        openDialog.Filter = this.FileExtensions;

                        if (openDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {
                            this.uriTextBox.Text = openDialog.FileName;

                            this.Location = this.uriTextBox.Text;
                        }
                    }
                    break;
                case UriType.Folder:
                    {
                        System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();

                        if (this.Location != null && this.Location != string.Empty)
                        {
                            folderDialog.SelectedPath = this.Location;

                            if (!Directory.Exists(folderDialog.SelectedPath))
                            {
                                Directory.CreateDirectory(folderDialog.SelectedPath);
                            }
                        }

                        if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {
                            this.uriTextBox.Text = folderDialog.SelectedPath;

                            this.Location = this.uriTextBox.Text;
                        }
                    }
                    break;
            }

            e.Handled = true;
        }

        private void uriTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            this.Location = this.uriTextBox.Text;
        }
    }
}
